#include "cellusermgr.h"
#include "ui_cellusermgr.h"

CellUserMgr::CellUserMgr(QWidget *parent)
    : QWidget(parent)
    , ui(new Ui::CellUserMgr)
{
    ui->setupUi(this);
    ui->tableView->setModel(&m_model);
    ui->tableView->setEditTriggers(QAbstractItemView::NoEditTriggers);//不能直接更改表格中的元素
    ui->tableView->setSelectionBehavior(QAbstractItemView::SelectRows);//每次选中会选中一行
    m_model.setHorizontalHeaderLabels(QStringList{"id","用户名","密码","身份","年级","部门"});
    ui->tableView->horizontalHeader()->setSectionResizeMode(QHeaderView::Stretch);//让m_model占满tableView
    // QList<QStandardItem*> items;
    // items.append(new QStandardItem());
    // m_model.appendRow(items);
}

CellUserMgr::~CellUserMgr()
{
    delete ui;
}

void CellUserMgr::initPage(QString strCondition)
{
    //查询数据库并且显示
    auto l=Sqlmgr::getinstance()->getUser(strCondition);//单例模式

    m_model.clear();//不加清除的话，会在来回切换几个页面时,重复刷出数据

    m_model.setHorizontalHeaderLabels(QStringList{"id","用户名","密码","身份","年级","部门"});
    for(int i=0;i<l.size();i++)
    {
        QList<QStandardItem*> items;
        for(int j=0;j<l[i].size();j++)
        {
            items.append(new QStandardItem(l[i][j]));
        }
        m_model.appendRow(items);
    }
}

void CellUserMgr::on_btn_del_clicked()//删除用户
{
    int r=ui->tableView->currentIndex().row();
    if(r<0)
    {
        QMessageBox::information(NULL,"删除","未选中用户");
    }
    else
    {
        auto id=m_model.item(r,0)->text();
        Sqlmgr::getinstance()->delUser(id);
        ui->le_search->clear();//清除一下搜索
        initPage();
    }
}

static QString convertUTF8(QString strGBK)
{
    QTextCodec *codec=QTextCodec::codecForName("GB18030");
    QByteArray encodedString = codec->fromUnicode(strGBK);
    QString strUTF8 = codec->toUnicode(encodedString);
    return strUTF8;
}


void CellUserMgr::on_btn_add_clicked()//导入用户
{
    auto strPath=QFileDialog::getOpenFileName(NULL,"输入文件路径");
    if(!strPath.isEmpty())
    {
        QFile f(strPath);
        f.open(QFile::ReadOnly);
        QVector<QStringList> vecData;
        while(!f.atEnd())
        {
            QString str=f.readLine();
            QStringList l=str.split(",");
            //gbk转UTF-8
            for(auto &itC:l)
            {
                itC=convertUTF8(itC);
            }
            // 去掉末尾的换行符
            for (auto &itC : l)
            {
                itC = itC.trimmed();
            }

            if(l.size()!=5)
            {
                QMessageBox::information(NULL,"信息","导入失败");
                return;
            }
            //l[l.size()-1]=l[l.size()-1].chopped(2);
            vecData.push_back(l);

        }
        Sqlmgr::getinstance()->addUser(vecData);
    }
    initPage();
}


void CellUserMgr::on_btn_update_clicked()//修改用户
{
      int r=ui->tableView->currentIndex().row();
    if(r<0)
      {
        QMessageBox::information(nullptr,"信息","无选中用户");
      }
    else
    {
        int id=m_model.item(r,0)->text().toUInt();
        Dlg_UpdateUser dlg;
        dlg.setType(id);
        dlg.exec();
    }
      initPage();
}


void CellUserMgr::on_btn_search_clicked()//用id搜索用户
{
    auto strId=ui->le_search->text();
    QString strCondition=QString("where id=%1").arg(strId);
    initPage(strCondition);
}


void CellUserMgr::on_le_search_textChanged(const QString &arg1)//用用户名搜索用户
{
    QString strCondition=QString("where username like '%%1%' ").arg(arg1);
    initPage(strCondition);
}

